#include "Alice/Math/OBBRect.h"

#include "Alice/Log/Log.h"

namespace Alice
{


OBBRect OBBRect::CalRotateRect(const OBBRect& rect, const Vec2& dot, float angle)
{
	OBBRect ret{
		Vec2::CalRotateDot(rect.left_top, dot, angle),
		Vec2::CalRotateDot(rect.right_top, dot, angle),
		Vec2::CalRotateDot(rect.right_bottom, dot, angle),
		Vec2::CalRotateDot(rect.left_bottom, dot, angle)
	};

	return ret;
}

Vec2 OBBRect::CalCenter(const OBBRect& rect)
{
	return Vec2{
		rect.left_top.x + (rect.right_bottom.x - rect.left_top.x) / 2,
		rect.left_top.y + (rect.right_bottom.y - rect.left_top.y) / 2
	};
}

Vec2 OBBRect::CalProjOnAxis(const OBBRect& rect, const Vec2& axis)
{
	float min = std::numeric_limits<float>::infinity();
	float max = -std::numeric_limits<float>::infinity();

	Vec2 vertices[4] = {
		rect.left_top, rect.right_top,
		rect.right_bottom, rect.left_bottom
	};

	for (auto& vertex : vertices)
	{
		float proj = Vec2::DotProduct(vertex, axis);
		if (proj < min) min = proj;
		if (proj > max) max = proj;
	}

	return Vec2{min, max};
}

std::array<Vec2, 4> OBBRect::CalRectAxes(const OBBRect& a, const OBBRect& b)
{
	std::array<Vec2, 4> axes;

	axes[0] = a.right_top - a.left_top;
	axes[1] = a.left_bottom - a.left_top;

	axes[2] = b.right_top - b.left_top;
	axes[3] = b.left_bottom - b.left_top;

	return axes; 
}

} // namespace Alice
